package org.leaf.green.b2b

class InventoryController {

    def index = { redirect(action: "list", params: params) }

    // the delete, save and update actions only accept POST requests
    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def list = {
        params.max = Math.min(params.max ? params.max.toInteger() : 100,  100)
        render view : '/b2b/inventory/list', model : [inventoryInstanceList: Inventory.list(params), inventoryInstanceTotal: Inventory.count()]
    }

    def create = {
        def inventoryInstance = new Inventory()
        inventoryInstance.properties = params
        render view : '/b2b/inventory/create', model : [inventoryInstance: inventoryInstance]
    }

    def save = {
        def inventoryInstance = new Inventory(params)
        if (!inventoryInstance.hasErrors() && inventoryInstance.save()) {
            flash.message = "inventory.created"
            flash.args = [inventoryInstance.id]
            flash.defaultMessage = "Inventory ${inventoryInstance.id} created"
            redirect(action: "show", id: inventoryInstance.id)
        }
        else {
            render(view: "/b2b/inventory/create", model: [inventoryInstance: inventoryInstance])
        }
    }

    def show = {
        def inventoryInstance = Inventory.get(params.id)
        if (!inventoryInstance) {
            flash.message = "inventory.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "Inventory not found with id ${params.id}"
            redirect(action: "list")
        }
        else {
            render view : '/b2b/inventory/show', model : [inventoryInstance: inventoryInstance]
        }
    }

    def edit = {
        def inventoryInstance = Inventory.get(params.id)
        if (!inventoryInstance) {
            flash.message = "inventory.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "Inventory not found with id ${params.id}"
            redirect(action: "list")
        }
        else {
            render view : '/b2b/inventory/edit', model : [inventoryInstance: inventoryInstance]
        }
    }

    def update = {
        def inventoryInstance = Inventory.get(params.id)
        if (inventoryInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (inventoryInstance.version > version) {
                    
                    inventoryInstance.errors.rejectValue("version", "inventory.optimistic.locking.failure", "Another user has updated this Inventory while you were editing")
                    render(view: "/b2b/inventory/edit", model: [inventoryInstance: inventoryInstance])
                    return
                }
            }
            inventoryInstance.properties = params
            if (!inventoryInstance.hasErrors() && inventoryInstance.save()) {
                flash.message = "inventory.updated"
                flash.args = [params.id]
                flash.defaultMessage = "Inventory ${params.id} updated"
                redirect(action: "show", id: inventoryInstance.id)
            }
            else {
                render(view: "/b2b/inventory/edit", model: [inventoryInstance: inventoryInstance])
            }
        }
        else {
            flash.message = "inventory.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "Inventory not found with id ${params.id}"
            redirect(action: "edit", id: params.id)
        }
    }

    def delete = {
        def inventoryInstance = Inventory.get(params.id)
        if (inventoryInstance) {
            try {
                inventoryInstance.delete()
                flash.message = "inventory.deleted"
                flash.args = [params.id]
                flash.defaultMessage = "Inventory ${params.id} deleted"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "inventory.not.deleted"
                flash.args = [params.id]
                flash.defaultMessage = "Inventory ${params.id} could not be deleted"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "inventory.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "Inventory not found with id ${params.id}"
            redirect(action: "list")
        }
    }
}
